package www.dianbaozu.com.ui.server.topping

import android.view.View
import androidx.fragment.app.viewModels
import com.netease.nim.uikit.business.session.module.Container
import com.netease.nim.uikit.business.session.module.ModuleProxy
import com.netease.nim.uikit.business.session.module.list.MessageListPanelEx
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum
import com.netease.nimlib.sdk.msg.model.IMMessage
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentToppingBinding
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.extension.navBack
import www.wen.com.common.lib.extension.observeObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter

/**
 * @author WEN
 * @Description:私信
 * @date 2020//25 17:48
 *
 * [Deprecated]
 * 使用IM自带的最近联系人页面 [com.netease.nim.uikit.business.recent.RecentContactsFragment]
 */
@AndroidEntryPoint
class ToppingFragment : BaseLoadingFragment<FragmentToppingBinding>(), View.OnClickListener,
    ModuleProxy {
    private val sessionId by lazy { requireActivity().intent.getStringExtra("sessionId") ?: "" }

    private var messageListPanel: MessageListPanelEx? = null

    private val toppingViewModel :ToppingViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = toppingViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter =
        TitleBarUniversalAdapter(requireActivity(), "置顶", action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_topping

    override fun lazyInitView() {
        parseIntent()
        initObserve()
        loadData()
    }

    private fun initObserve() {
        observeObject(KeyHelper.TOPPING_MSG_CHANGE_KEY,this,IMMessage::class.java){
            messageListPanel!!.removeMessage(this)
        }
    }

    private fun loadData() {
        toppingViewModel.getServerChannelTopMsgList(sessionId){
            if (it.data.isNotEmpty()) {
                messageListPanel!!.forceIncomingMessage(it.data)
            }
        }
    }

    private fun parseIntent() {
        val container = Container(
            activity, "", SessionTypeEnum.P2P, this, true
        )
        if (messageListPanel == null) {
            messageListPanel = MessageListPanelEx(container, dataBinding.root, null, false, false,false,true)
        } else {
            messageListPanel!!.reload(container, null)
        }
    }

    override fun onClick(v: View) {
        navBack()
    }

    override fun onDestroy() {
        super.onDestroy()
        messageListPanel?.onDestroy()
    }

    override fun sendMessage(msg: IMMessage?): Boolean = false

    override fun onInputPanelExpand() {}

    override fun shouldCollapseInputPanel() {}

    override fun isLongClickEnabled(): Boolean = true

    override fun onItemFooterClick(message: IMMessage?) {}

    override fun onReplyMessage(replyMsg: IMMessage?) {}

}



